//
//  CityServiceViewController.m
//  cityStar
//
//  Created by Hancong on 15/1/21.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "CityServiceViewController.h"
#import "HTDefine.h"
#import "ServerUpCollectionViewCell.h"
#import "ServiceHeaderCollectionReusableView.h"
#import "CarpoolingViewController.h"
#import "MakefriendsViewController.h"
#import "VideoViewController.h"
#import "OtherViewController.h"
#import "RecruitmentViewController.h"
#import "HomeEconomicsListViewController.h"
#import "LawViewController.h"
#import "HWSearchViewController.h"
#import "OtherViewController.h"

NSString *const serverUpCollectionViewCellID = @"ServerUpCollectionViewCellID";
NSString *const serviceHeaderCollectionViewCellID = @"ServiceHeaderCollectionViewCellID";

@interface CityServiceViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,ServiceHeaderDelegate>

@property (strong, nonatomic) IBOutlet UICollectionView *collectView;
@property (nonatomic, strong) NSMutableArray *requestArray;
@property (nonatomic, strong) NSMutableArray *requestBelowArray;

@end

@implementation CityServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpView];
    [self webRequest_GetServiceMessage];
    [self setupCollectionView];
}

#pragma mark - webRequest
-(void)webRequest_GetServiceMessage
{
    [HttpRequestManager getMethod:KinterFace_ServiceShop parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.requestArray = response[@"value"];
            
            [HttpRequestManager getMethod:KinterFace_ServiceRelease parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                DLog(@"%@",response);
                self.requestBelowArray = response[@"value"];
                [self.collectView reloadData];
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

#pragma mark - setUps
-(void)setUpView
{
    [self addBackButton];
    self.title = @"城市服务";
    UIButton * regBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    regBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [regBtn setTitleColor:rgba(133, 133, 133, 1) forState:UIControlStateHighlighted];
    [regBtn setImage:[UIImage imageNamed:@"bsearch"] forState:UIControlStateNormal];
    [regBtn setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    [regBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * iHaveNewsBtnItem = [[UIBarButtonItem alloc]initWithCustomView:regBtn];
    self.navigationItem.rightBarButtonItem = iHaveNewsBtnItem;
}

-(void)setupCollectionView
{
    UICollectionViewFlowLayout *flowout = [[UICollectionViewFlowLayout alloc]init];
    flowout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(102, 370, 199, 66) collectionViewLayout:flowout];
    self.collectView.backgroundColor = [UIColor whiteColor];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
//    [self.view addSubview:self.collectView];
    
    [self.collectView registerNib:[ServerUpCollectionViewCell nib] forCellWithReuseIdentifier:serverUpCollectionViewCellID];
    [self.collectView registerNib:[ServiceHeaderCollectionReusableView nib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:serviceHeaderCollectionViewCellID];

}

- (void)searchAction
{
    HWSearchViewController *hwSearchVC = [[HWSearchViewController alloc]init];
    hwSearchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:hwSearchVC animated:YES];
}

#pragma mark - CollectView Delegate and dataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

//每个区内的元素个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.requestArray.count;
    }else{
    return self.requestBelowArray.count;
    }
}

//设置元素内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ServerUpCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:serverUpCollectionViewCellID forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell configWithCellMessage:self.requestArray[indexPath.row] andOther:NO];
    }else{
        [cell configWithCellMessage:self.requestBelowArray[indexPath.row] andOther:YES];
    }
    
    return cell;
}
//设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(100,45);
}


//类似tableview的header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ServiceHeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:serviceHeaderCollectionViewCellID forIndexPath:indexPath];
        headView.delegate = self;
        return headView;
    }else{
        return nil;
    }

}


#pragma mark - UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"%li",indexPath.row);
    
    if (indexPath.section == 1) {
        OtherViewController *otherVC = [[OtherViewController alloc]init];
        [self.navigationController pushViewController:otherVC animated:YES];
    }
//    ScoreExchange *normalScoreExchange = self.normalExchangesPageInfo.objectList[indexPath.row];
//    OrangeDetailsVC *orangeDetailsVC = [[OrangeDetailsVC alloc] initWithScoreExchange:normalScoreExchange];
//    [self.navigationController pushViewController:orangeDetailsVC animated:YES];
}

#pragma mark - UICollectionViewDelegate Flow Layout
//类似tableview的header
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [ServiceHeaderCollectionReusableView sizeOfReusableView];
    }else
    {
        return CGSizeMake(0, 0);
    }
}

#pragma mark - serviceHeaderViewDelegate
-(void)serviceHeaderSelectButtonWithTag:(NSInteger)tag
{
    switch (tag) {
        case 1:
        {
            VideoViewController *videoVC = [[VideoViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 2:
        {
            MakefriendsViewController *videoVC = [[MakefriendsViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 3:
        {
            RecruitmentViewController *videoVC = [[RecruitmentViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 4:
        {
            HomeEconomicsListViewController *videoVC = [[HomeEconomicsListViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 5:
        {
            BossSystemShowAlertViewWithTitle(@"公厕没有界面");
//            DLog(@"公厕没有界面")
//            VideoViewController *videoVC = [[VideoViewController alloc]init];
//            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 6:
        {
            BossSystemShowAlertViewWithTitle(@"这是其他");
            OtherViewController *videoVC = [[OtherViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 7:
        {
            LawViewController *videoVC = [[LawViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
        case 8:
        {
            CarpoolingViewController *videoVC = [[CarpoolingViewController alloc]init];
            [self.navigationController pushViewController:videoVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
